#include <iostream>
#include <cstring>
#include <queue>
#define MAXN 1005
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> Node;

int N, M, T;
int dist[MAXN][MAXN];
int dp[MAXN];
int dp2[MAXN];

void solve();

void dijstra(int from);

void dijstra2(int from);

int main() {
    memset(dist, INF, sizeof dist);
    cin >> N >> M >> T;
    int a, b, c;
    for (int i = 0; i < M; i++) {
        cin >> a >> b >> c;
        dist[a][b] = c;
        dist[a][a] = 0; dist[b][b] = 0;
    }
    solve();
}


void dijstra(int from) {
    memset(dp, INF, sizeof dp);
    dp[from] = 0;
    priority_queue<Node, vector<Node>, greater<Node> > q;
    q.push(make_pair(0, from));
    while (!q.empty()) {
        Node n = q.top(); q.pop();
        int d = n.first, v = n.second;
        if (d > dp[v]) continue;
        for (int i = 1; i <= N; i++) {
            if (dp[i] > d + dist[v][i]) {
                dp[i] = d + dist[v][i];
                q.push(make_pair(dp[i], i));
            }
        }
    }
}

void dijstra2(int from) {
    memset(dp2, INF, sizeof dp2);
    dp2[from] = 0;
    priority_queue<Node, vector<Node>, greater<Node> > q;
    q.push(make_pair(0, from));
    while (!q.empty()) {
        Node n = q.top(); q.pop();
        int d = n.first, v = n.second;
        if (d > dp2[v]) continue;
        for (int i = 1; i <= N; i++) {
            if (dp2[i] > d + dist[i][v]) {
                dp2[i] = d + dist[i][v];
                q.push(make_pair(dp2[i], i));
            }
        }
    }
}

void solve() {
    dijstra(T);
    dijstra2(T);
    int res = 0;
    for (int i = 1; i <= N; i++)
        res = max(dp2[i] + dp[i], res);
    cout << res << endl;
}